﻿var ItemCount = 0;
var OrderItemInfo = "";
$(document).ready(function () {
    OrderItemInfo = $.parseJSON($("#hid_today_item_list").val());
    ItemCount = OrderItemInfo["ItemCount"];
    bindTiemList(OrderItemInfo["ItemList"]);
    $('#tbl_menu_list').tablePagination();

    var currDate = new Date();
    if ((currDate.getHours() == 17 && currDate.getMinutes() > 30) || currDate.getHours() > 17) {
        alert("现在订餐无效，请与管理员联系！");
    }
});

function bindTiemList(dataScr) {
    var currTbl = $("#tbl_item_list>tbody");
    currTbl.empty();
    $(dataScr).each(function () {
        var rowTr = "<tr><td style='width: 220px;'>" + this["NAME"] + "</td><td style='width: 120px;'>" + this["RESTURANT"] + "</td><td style='width: 220px;'>" + this["DINNER"] + "</td><td style='width: 190px;'>" + this["REMARK"] + "</td></tr>";
        $(currTbl).append(rowTr);
    });
    if (dataScr.length > 0) {
        currTbl.closest("div").show();
    }
}

function orderItem(obj) {

    if (ItemCount == 1) {
        if (!confirm("还不够吗？是否真的要再吃一份？")) {
            return;
        }
    }
    if (ItemCount == 2) {
        alert("你吃的太多了！注意健康饮食，保持良好身材！");
        return;
    }
   
    var currTr = $(obj).closest("tr");

    var data = new QueryData("Users/UserProcess.ashx", "OrderNewItem");
    data.SetQueryData(currTr, "order_info");
    data.Data["order_info"]["resturant_name"] = $("#ddl_resturant").val();
    var json = $.toJSON(data.Data);
    $.ajax({
        type: "POST",
        url: data.URL,
        dataType: "json",
        data: $.toJSON(data.Data),
        success: function (result) {
            if (!checkAjaxResult(result)) {
                return;
            }
            if (result["Result"] != "True") {
                ItemCount = result["ItemCount"];
                bindTiemList(result["ItemList"]);
            }
            else {
                alert("订餐失败！请刷新页面后重试，或与管理员联系！");
            }
        },
        error: function (result) {
        }
    });
}